<html autopagermatchedrules="1"><head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Multithreading and Thread Synchronization</title>
<style>
<!--
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";
	color:windowtext;}
h2
	{margin-right:0cm;
	margin-left:0cm;
	font-size:18.0pt;
	font-family:"Times New Roman";
	color:black;
	font-weight:bold;}
h3
	{margin-right:0cm;
	margin-left:0cm;
	font-size:13.5pt;
	font-family:"Times New Roman";
	color:black;
	font-weight:bold;}
a:link, span.MsoHyperlink
	{color:windowtext;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
p
	{margin-right:0cm;
	margin-left:0cm;
	font-size:12.0pt;
	font-family:"Times New Roman";
	color:black;}
pre
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.0pt;
	font-family:"Courier New";
	color:black;}
tt
	{font-family:"Courier New";}
p.para, li.para, div.para
	{margin-right:0cm;
	margin-left:0cm;
	font-size:12.0pt;
	font-family:"Times New Roman";
	color:black;}
@page Section1
	{size:21.0cm 842.0pt;
	margin:1.0cm 1.0cm 1.0cm 1.0cm;}
div.Section1
	{page:Section1;}
 /* List Definitions */
 ol
	{margin-bottom:0cm;}
ul
	{margin-bottom:0cm;}
-->
</style>

</head>

<body lang="BG" link="#000000" vlink="purple">

<div class="Section1">

<h2 style="margin-top:0cm;margin-right:0cm;margin-bottom:6.0pt;
margin-left:0cm;text-align:center" align="center"><span style="font-size:16.0pt;
font-family:&quot;Courier New&quot;;color:windowtext" lang="EN-US">Internet Programming with Java
Course</span></h2>

<h2 style="margin-top:0cm;margin-right:0cm;margin-bottom:6.0pt;
margin-left:0cm;text-align:center" align="center"><span style="font-size:16.0pt;
color:windowtext" lang="EN-US">1.3. Multithreading and Thread Synchronization</span></h2>

<h2><span style="color:windowtext" lang="EN-US">Understanding Multithreading</span></h2>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">All the
sample programs you developed in the preceding chapters have had only a single
thread of execution. Each program proceeded sequentially, one instruction after
another, until it completed its processing and terminated. </span></p>

<p style="text-align:justify"><i><span style="color:windowtext" lang="EN-US">Multithreaded</span></i><span style="color:windowtext" lang="EN-US"> programs are similar to the
single-threaded programs that you have been studying. They differ only in the
fact that they support more than one <i>concurrent</i> thread of execution-that
is, they are able to simultaneously execute multiple sequences of instructions.
Each instruction sequence has its own unique flow of control that is
independent of all others. These independently executed instruction sequences
are known as <i>threads</i>. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">If your
computer has only a single CPU, you might be wondering how it can execute more
than one thread at the same time. In single-processor systems, only a single
thread of execution occurs at a given instant. The CPU quickly switches back
and forth between several threads to create the illusion that the threads are
executing at the same time. Single-processor systems support <i>logical
concurrency</i>, not <i>physical concurrency</i>. Logical concurrency is the
characteristic exhibited when multiple threads execute with separate,
independent flows of control. On multiprocessor systems, several threads do, in
fact, execute at the same time, and physical concurrency is achieved. The
important feature of multithreaded programs is that they support logical concurrency,
not whether physical concurrency is actually achieved. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">Many
programming languages support <i>multiprogramming</i>. Multiprogramming is the
logically concurrent execution of multiple programs. For example, a program can
request that the operating system execute programs A, B, and C by having it
spawn a separate process for each program. These programs can run in parallel,
depending upon the multiprogramming features supported by the underlying
operating system. Multithreading differs from multiprogramming in that
multithreading provides concurrency within the context of a single process and
multiprogramming provides concurrency between processes. Threads are not
complete processes in and of themselves. They are a separate flow of control
that occurs within a process. <a href="http://www.nakov.com/inetjava/lectures/part-1-sockets/InetJava%20BooksThreadstemp123Java%20Developer%27s%20Guide%20%282%29f8-1.gif">Figure
8.1</a> illustrates the difference between multithreading and multiprogramming.
</span></p>

<p style="text-align:center" align="center"><span style="color:windowtext" lang="EN-US"><img src="Multithreading%20and%20Thread%20Synchronization_files/image001.jpg" border="0" height="262" width="182"></span></p>

<p style="text-align:center" align="center"><span style="color:windowtext" lang="EN-US"><a href="http://www.nakov.com/inetjava/lectures/part-1-sockets/InetJava%20BooksThreadstemp123Java%20Developer%27s%20Guide%20%282%29f8-1.gif"><b>Figure
8.1 : </b><i>Multithreading versus multiprogramming.</i></a></span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">An
executing program is generally associated with a single process. The advantage
of multithreading is that concurrency can be used within a process to provide
multiple simultaneous services to the user. Multithreading also requires less
processing overhead than multiprogramming because concurrent threads are able
to share common resources more easily. Multiple executing programs tend to
duplicate resources and share data as the result of more time-consuming
interprocess communication. </span></p>

<h2><span style="color:windowtext" lang="EN-US">How Java Supports Multithreading</span></h2>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">Java's
multithreading support is centered around the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">java.lang.Thread</span></tt><span style="color:windowtext" lang="EN-US"> class. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> class provides the capability to create objects of
class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">, each with its own separate flow of
control. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> class encapsulates the data and methods
associated with separate threads of execution and allows multithreading to be
integrated within the object-oriented framework. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">Java
provides two approaches to creating threads. In the first approach, you create
a subclass of class </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">
and override the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US">
method to provide an entry point into the thread's execution. When you create
an instance of your </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">
subclass, you invoke its </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">start()</span></tt><span style="color:windowtext" lang="EN-US">
method to cause the thread to execute as an independent sequence of
instructions. The </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">start()</span></tt><span style="color:windowtext" lang="EN-US">
method is inherited from the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">
class. It initializes the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">
object using your operating system's multithreading capabilities and invokes
the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US"> method. You learn how to create threads
using this approach in the next section.</span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The
approach to creating threads identified in the previous paragraph is very
simple and straightforward. However, it has the drawback of requiring your </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> objects to be under the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> class in the class hierarchy. In some
cases, as you'll see when you study applets in Part VI, "Programming the
Web with Applets and Scripts," this requirement can be somewhat limiting. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">Java's
other approach to creating threads does not limit the location of your </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> objects within the class hierarchy. In
this approach, your class implements the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">java.lang.Runnable</span></tt><span style="color:windowtext" lang="EN-US"> interface. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Runnable</span></tt><span style="color:windowtext" lang="EN-US"> interface consists of a single method, the
</span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US"> method, which must be overridden by your
class. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US"> method provides an entry point into your
thread's execution. In order to run an object of your class as an independent
thread, you pass it as an argument to a constructor of class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">. You learn how to create threads using
this approach later in this chapter in the section titled "Implementing </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Runnable</span></tt><span style="color:windowtext" lang="EN-US">." </span></p>

<h3><span style="color:windowtext" lang="EN-US">Creating Subclasses of </span><tt><span style="color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> </span></h3>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">In this
section, you create your first multithreaded program by creating a subclass of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> and then creating, initializing, and
starting two </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> objects from your class. The threads will
execute concurrently and display </span><tt><span style="font-size:
10.0pt;color:windowtext" lang="EN-US">Java is hot, aromatic, and invigorating.</span></tt><span style="color:windowtext" lang="EN-US"> to the console window. </span></p>

<p class="MsoNormal" style="margin-right:36.0pt"><b><span lang="EN-US">The source
code of the </span></b><tt><b><span style="font-size:10.0pt" lang="EN-US">ThreadTest1</span></b></tt><b><span lang="EN-US"> program.</span></b><span lang="EN-US"> </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> ThreadTest1 </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">static</span> <span style="color:blue">void</span>
main(String args[]) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyThread thread1 = <span style="color:blue">new</span> MyThread(<span style="color:green">"thread1:
"</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyThread thread2 = <span style="color:blue">new</span> MyThread(<span style="color:green">"thread2:
"</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread1.start();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread2.start();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean
thread1IsAlive = <span style="color:blue">true</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean
thread2IsAlive = <span style="color:blue">true</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">do</span> {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">if </span>(thread1IsAlive &amp;&amp; !thread1.isAlive()) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread1IsAlive
= <span style="color:blue">false</span>;</span></p>

<p class="MsoNormal" style="margin-left:68.1pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">System.<span style="color:blue">out</span>.println(<span style="color:green">"Thread 1
is dead."</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">if </span>(thread2IsAlive &amp;&amp; !thread2.isAlive()) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread2IsAlive
= <span style="color:blue">false</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Thread 2
is dead."</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <span style="color:blue">while</span>(thread1IsAlive || thread2IsAlive);</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">}</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> MyThread extends
Thread </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{</span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">static</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> String message[]
= </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ <span style="color:green">"Java"</span>, <span style="color:green">"is"</span>,
<span style="color:green">"hot,"</span>, <span style="color:green">"aromatic,"</span>,
<span style="color:green">"and"</span>, <span style="color:green">"invigorating."</span>};</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
MyThread(String id) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(id);</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">void</span> run() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String name =
getName();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">for </span>(<span style="color:blue">int</span> i=<span style="color:navy">0</span>;i&lt;message.length;++i) {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; randomWait();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(name + message[i]);</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">void</span>
randomWait()</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">try</span> {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep((<span style="color:blue">long</span>)(<span style="color:navy">3000</span>*Math.random()));</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <span style="color:blue">catch</span> (InterruptedException x) {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Interrupted!"</span>);
</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">This
program creates two threads of execution, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:
10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US">,
from the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyThread</span></tt><span style="color:windowtext" lang="EN-US"> class. It then starts both threads and
executes a </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">do</span></tt><span style="color:windowtext" lang="EN-US"> statement that waits for the threads to
die. The threads display the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">Java is hot, aromatic, and invigorating.</span></tt><span style="color:windowtext" lang="EN-US"> message word by word, while waiting a
short, random amount of time between each word. Because both threads share the
console window, the program's output identifies which threads were able to
write to the console at various times during the program's execution.</span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">Run </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US"> to get an idea of the output that it
produces. Each time you run the program you might get a different program
display. This is because the program uses a random number generator to
determine how long each thread should wait before displaying its output. Look
at the following output: </span></p>

<p class="MsoNormal" style="margin-right:36.0pt;margin-left:36.0pt"><tt><span style="font-size:10.0pt" lang="EN-US">C:\java\jdg\ch08&gt;java ThreadTest1</span></tt><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"><br>
<tt>thread1: Java</tt><br>
<tt>thread2: Java</tt><br>
<tt>thread2: is</tt><br>
<tt>thread2: hot,</tt><br>
<tt>thread2: aromatic,</tt><br>
<tt>thread1: is</tt><br>
<tt>thread1: hot,</tt><br>
<tt>thread2: and</tt><br>
<tt>thread1: aromatic,</tt><br>
<tt>thread1: and</tt><br>
<tt>thread2: invigorating.</tt><br>
<tt>Thread 2 is dead.</tt><br>
<tt>thread1: invigorating.</tt><br>
<tt>Thread 1 is dead.</tt></span><span lang="EN-US"> </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">This
output shows that </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US">
executed first and displayed </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">Java</span></tt><span style="color:windowtext" lang="EN-US"> to
the console window. It then waited to execute while </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> displayed </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Java</span></tt><span style="color:windowtext" lang="EN-US">, </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">is</span></tt><span style="color:windowtext" lang="EN-US">, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">hot,</span></tt><span style="color:windowtext" lang="EN-US">, and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">aromatic,</span></tt><span style="color:windowtext" lang="EN-US">. After that, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> waited while </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> continued its execution. </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> displayed </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">is</span></tt><span style="color:windowtext" lang="EN-US"> and then </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">hot,</span></tt><span style="color:windowtext" lang="EN-US">. At this point, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> took over again. </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> displayed </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">and</span></tt><span style="color:windowtext" lang="EN-US"> and then went back into waiting. </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> then displayed </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">aromatic,</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">and</span></tt><span style="color:windowtext" lang="EN-US">. </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US">
finished its execution by displaying </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">invigorating.</span></tt><span style="color:windowtext" lang="EN-US">. Having completed its execution, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> died, leaving </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> as the only executing task. </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1 </span></tt><span style="color:windowtext" lang="EN-US">displayed </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">invigorating.</span></tt><span style="color:windowtext" lang="EN-US"> and then completed its execution. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US"> class consists of a single </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">main()</span></tt><span style="color:windowtext" lang="EN-US"> method. This method begins by creating </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> as new objects of class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyThread</span></tt><span style="color:windowtext" lang="EN-US">. It then starts both threads using the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">start()</span></tt><span style="color:windowtext" lang="EN-US"> method. At this point, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">main()</span></tt><span style="color:windowtext" lang="EN-US"> enters a </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">do</span></tt><span style="color:windowtext" lang="EN-US"> loop that continues until both </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> are no longer alive. The loop monitors the execution
of the two threads and displays a message when it has detected the death of
each thread. It uses the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">isAlive()</span></tt><span style="color:windowtext" lang="EN-US">
method of the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> class to tell when a thread has died. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1IsAlive</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2IsAlive</span></tt><span style="color:windowtext" lang="EN-US"> variables are used to ensure that a
thread's obituary is only displayed once. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyThread</span></tt><span style="color:windowtext" lang="EN-US"> class extends class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">. It declares a statically initialized
array, named </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">message[]</span></tt><span style="color:windowtext" lang="EN-US">, that contains the message to be displayed
by each thread. It has a single constructor that invokes the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> class constructor via </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">super()</span></tt><span style="color:windowtext" lang="EN-US">. It contains two access methods: </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">randomWait()</span></tt><span style="color:windowtext" lang="EN-US">. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US"> method is required. It uses the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">getName()</span></tt><span style="color:windowtext" lang="EN-US"> method of class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> to get the name of the currently executing
thread. It then prints each word of the output display message while waiting a
random length of time between each print. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">randomWait()</span></tt><span style="color:windowtext" lang="EN-US"> method invokes the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">sleep()</span></tt><span style="color:windowtext" lang="EN-US"> method within a </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">try</span></tt><span style="color:windowtext" lang="EN-US"> statement. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">sleep()</span></tt><span style="color:windowtext" lang="EN-US"> method is another method inherited from
class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">. It causes the currently executing task to
"go to sleep" or wait until a randomly specified number of
milliseconds has transpired. Because the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">sleep()</span></tt><span style="color:windowtext" lang="EN-US"> method throws the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">InterruptedException</span></tt><span style="color:windowtext" lang="EN-US"> when its sleep is interrupted (how
grouchy!), the exception is caught and handled by the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">randomWait()</span></tt><span style="color:windowtext" lang="EN-US"> method. The exception is handled by
displaying the fact that an interruption has occurred to the console window. </span></p>

<h3><span style="color:windowtext" lang="EN-US">Implementing </span><tt><span style="color:windowtext" lang="EN-US">Runnable</span></tt><span style="color:windowtext" lang="EN-US"> </span></h3>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">In the
previous section, you created a multithreaded program by creating the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyThread</span></tt><span style="color:windowtext" lang="EN-US"> subclass of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">. In this section, you create a program with similar
behavior, but you create your threads as objects of the class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US">, which is not a subclass of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">. </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US"> will implement the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Runnable</span></tt><span style="color:windowtext" lang="EN-US"> interface and objects of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US"> will be executed as threads by passing them as
arguments to the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">
constructor. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> program's source code is shown in Listing
8.2. Enter it into the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">ThreadTest2.java</span></tt><span style="color:windowtext" lang="EN-US"> file and compile it. </span></p>

<p class="MsoNormal" style="margin-right:36.0pt"><b><span lang="EN-US">The source
code of the </span></b><tt><b><span style="font-size:10.0pt" lang="EN-US">ThreadTest2</span></b></tt><b><span lang="EN-US"> program.</span></b><span lang="EN-US"> </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> ThreadTest2 </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">static</span> <span style="color:blue">void</span>
main(String args[]) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread thread1 = <span style="color:blue">new</span> Thread(<span style="color:blue">new</span>
MyClass(<span style="color:green">"thread1: "</span>));</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread thread2 = <span style="color:blue">new</span> Thread(<span style="color:blue">new</span>
MyClass(<span style="color:green">"thread2: "</span>));</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread1.start();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread2.start();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean
thread1IsAlive = <span style="color:blue">true</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean
thread2IsAlive = <span style="color:blue">true</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">do</span> {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">if </span>(thread1IsAlive &amp;&amp; !thread1.isAlive()) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread1IsAlive
= <span style="color:blue">false</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Thread 1
is dead."</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">if </span>(thread2IsAlive &amp;&amp; !thread2.isAlive()) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread2IsAlive
= <span style="color:blue">false</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Thread 2
is dead."</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <span style="color:blue">while</span>(thread1IsAlive || thread2IsAlive);</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">}</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> MyClass
implements Runnable </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{</span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">static</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> String message[]
= </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ <span style="color:green">"Java"</span>, <span style="color:green">"is"</span>,
<span style="color:green">"hot,"</span>, <span style="color:green">"aromatic,"</span>,
<span style="color:green">"and"</span>, <span style="color:green">"invigorating."</span>};</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; String name;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
MyClass(String id) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = id;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">void</span> run() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">for</span>(<span style="color:blue">int</span> i=<span style="color:navy">0</span>;i&lt;message.length;++i) {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; randomWait();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(name+message[i]);</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">void</span>
randomWait()</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">try</span> {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread.currentThread().sleep((<span style="color:blue">long</span>)(<span style="color:navy">3000</span>*Math.random()));
</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <span style="color:blue">catch</span> (InterruptedException x) {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Interrupted!"</span>);
</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> program is very similar to </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US">. It differs only in the way that the
threads are created. You should run </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> a few times to examine its output. Here
are the results of a sample run I made on my computer: </span></p>

<p class="MsoNormal" style="margin-right:36.0pt;margin-left:36.0pt"><tt><span style="font-size:10.0pt" lang="EN-US">C:\java\jdg\ch08&gt;java ThreadTest2</span></tt><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"><br>
<tt>thread2: Java</tt><br>
<tt>thread1: Java</tt><br>
<tt>thread2: is</tt><br>
<tt>thread2: hot,</tt><br>
<tt>thread1: is</tt><br>
<tt>thread2: aromatic,</tt><br>
<tt>thread1: hot,</tt><br>
<tt>thread1: aromatic,</tt><br>
<tt>thread1: and</tt><br>
<tt>thread2: and</tt><br>
<tt>thread1: invigorating.</tt><br>
<tt>Thread 1 is dead.</tt><br>
<tt>thread2: invigorating.</tt><br>
<tt>Thread 2 is dead.</tt></span><span lang="EN-US"> </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">These
results show </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> beginning its output before </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US">. It does not mean that </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> began executing before </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US">. </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread1</span></tt><span style="color:windowtext" lang="EN-US"> executed first, but went to sleep before generating
any output. </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread2</span></tt><span style="color:windowtext" lang="EN-US"> then executed and started its output
display before going to sleep. You can follow these results on your own to
analyze how </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> switched back and forth during their execution to
display their results to the console window. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">main()</span></tt><span style="color:windowtext" lang="EN-US"> method of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> differs from that of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US"> in the way that it creates </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US">. </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US">
created the threads as new instances of the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyThread</span></tt><span style="color:windowtext" lang="EN-US"> class. </span><tt><span style="font-size:
10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> was not able to create the threads directly, because </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US"> is not a subclass of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">. Instead, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> first created instances of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US"> and then passed them to the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread()</span></tt><span style="color:windowtext" lang="EN-US"> constructor, creating instances of class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US">. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread()</span></tt><span style="color:windowtext" lang="EN-US"> constructor used by </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> takes as its argument any class that
implements the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Runnable</span></tt><span style="color:windowtext" lang="EN-US"> interface. This is an example of the
flexibility and multiple-inheritance features provided by Java interfaces. The
rest of the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">main()</span></tt><span style="color:windowtext" lang="EN-US"> method is the same as that of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US">. </span></p>

<p style="text-align:justify"><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US">
is declared as implementing the </span><tt><span style="font-size:
10.0pt;color:windowtext" lang="EN-US">Runnable</span></tt><span style="color:
windowtext" lang="EN-US"> interface. This is a simple interface to implement; it only
requires that you implement the </span><tt><span style="font-size:
10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US">
method. </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US"> declares the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">name</span></tt><span style="color:windowtext" lang="EN-US"> variable to hold the names of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US"> objects that are created. In the first
example, the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyThread</span></tt><span style="color:windowtext" lang="EN-US"> class did not need to do this because a
thread-naming capability was provided by </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> and inherited by </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyThread</span></tt><span style="color:windowtext" lang="EN-US">. </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">MyClass</span></tt><span style="color:windowtext" lang="EN-US">
contains a simple constructor that initializes the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">name</span></tt><span style="color:windowtext" lang="EN-US"> variable. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US"> methods of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US"> are nearly identical, differing only with
respect to the name issue. This is also true of the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">randomWait()</span></tt><span style="color:windowtext" lang="EN-US"> method. In </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US">, the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">randomWait()</span></tt><span style="color:windowtext" lang="EN-US"> method must use the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">currentThread()</span></tt><span style="color:windowtext" lang="EN-US"> method of class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> to acquire a reference to an instance of
the current thread in order to invoke its </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">sleep()</span></tt><span style="color:windowtext" lang="EN-US"> method. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">Because
these two examples are so similar, you might be wondering why you would pick
one approach to creating a class over another. The advantage of using the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Runnable</span></tt><span style="color:windowtext" lang="EN-US"> interface is that your class does not need
to extend the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> class. This will be very helpful feature
when you start using multithreading in applets in Part VI of this book. The
only disadvantages to this approach are ones of convenience. You have to do a
little more work to create your threads and to access their methods. </span></p>

<h2><span style="color:windowtext" lang="EN-US">Thread States</span></h2>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">You
have now learned how to declare, create, initialize, start, and run Java
threads. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> programs also introduced you to the
concept of a thread's death. Threads transition through several states from the
time they are created until the time of their death. This section reviews these
states. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">A
thread is created by creating a new object of class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> or of one of its subclasses. When a thread
is first created, it does not exist as an independently executing set of
instructions. Instead, it is a template from which an executing thread will be
created. It first executes as a thread when it is started using the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">start()</span></tt><span style="color:windowtext" lang="EN-US"> method and run via the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US"> method. Before a thread is started it is
said to be in the <i>new thread</i> state. After a thread is started, it is in
the <i>runnable</i> state. When a class is in the runnable state, it may be
executing or temporarily waiting to share processing resources with other
threads. A runnable thread enters an extended wait state when one of its
methods is invoked that causes it to drop from the runnable state into a not
runnable state. In the <i>not runnable</i> state, a thread is not just waiting
for its share of processing resources, but is blocked waiting for the
occurrence of an event that will send it back to the runnable state. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">For
example, the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">sleep()</span></tt><span style="color:windowtext" lang="EN-US"> method was invoked in the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest2</span></tt><span style="color:windowtext" lang="EN-US"> programs to cause a thread to wait for a
short period of time so that the other thread could execute. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">sleep()</span></tt><span style="color:windowtext" lang="EN-US"> method causes a thread to enter the not
runnable state until the specified time has expired. A thread may also enter
the not runnable state while it is waiting for I/O to be completed, or as the
result of the invocation of other methods. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">A
thread leaves the not runnable state and returns to the runnable state when the
event that it is waiting for has occurred. For example, a sleeping thread must
wait for its specified sleep time to occur. A thread that is waiting on I/O
must wait for the I/O operation to be completed. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">A
thread may transition from the new thread, runnable, or not runnable state to
the <i>dead</i> state when its </span><tt><span style="font-size:
10.0pt;color:windowtext" lang="EN-US">stop()</span></tt><span style="color:windowtext" lang="EN-US">
method is invoked or the thread's execution is completed. When a thread enters
the dead state, it's a goner. It can't be revived and returned to any other
state. </span></p>

<h2><span style="color:windowtext" lang="EN-US">Thread Priority and Scheduling</span></h2>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">From an
abstract or a logical perspective, multiple threads execute as concurrent
sequences of instructions. This may be physically true for multiprocessor
systems, under certain conditions. However, in the general case, multiple
threads do not always physically execute at the same time. Instead, the threads
share execution time with each other based on the availability of the system's
CPU (or CPUs). </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The
approach used to determining which threads should execute at a given time is
referred to as <i>scheduling</i>. Scheduling is performed by the Java runtime
system. It schedules threads based on their <i>priority</i>. The
highest-priority thread that is in the runnable state is the thread that is run
at any given instant. The highest-priority thread continues to run until it
enters the death state, enters the not runnable state, or has its priority
lowered, or when a higher-priority thread becomes runnable. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">A
thread's priority is an integer value between </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MIN_PRIORITY</span></tt><span style="color:windowtext" lang="EN-US"> and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MAX_PRIORITY</span></tt><span style="color:windowtext" lang="EN-US">. These constants are defined in the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> class. In Java 1.0, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MIN_PRIORITY</span></tt><span style="color:windowtext" lang="EN-US"> is 1 and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MAX_PRIORITY</span></tt><span style="color:windowtext" lang="EN-US"> is 10. A thread's priority is set when it
is created. It is set to the same priority as the thread that created it. The
default priority of a thread is </span><tt><span style="font-size:
10.0pt;color:windowtext" lang="EN-US">NORM_PRIORITY</span></tt><span style="color:windowtext" lang="EN-US"> and is equal to 5. The priority of a thread can be
changed using the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">setPriority()</span></tt><span style="color:windowtext" lang="EN-US">
method. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">Java's
approach to scheduling is referred to as <i>preemptive scheduling</i>. When a
thread of higher priority becomes runnable, it preempts threads of lower
priority and is immediately executed in their place. If two or more
higher-priority threads become runnable, the Java scheduler alternates between
them when allocating execution time. </span></p>

<h2><span style="color:windowtext" lang="EN-US">Synchronization </span></h2>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">There
are many situations in which multiple threads must share access to common
objects. For example, all of the programs in this chapter have illustrated the
effects of multithreading by having multiple executing threads write to the
Java console, a common shared object. These examples have not required any
coordination or synchronization in the way the threads access the console
window: Whatever thread was currently executing was able to write to the
console window. No coordination between concurrent threads was required. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">There
are times when you might want to coordinate access to shared resources. For
example, in a database system, you might not want one thread to be updating a
database record while another thread is trying to read it. Java enables you to
coordinate the actions of multiple threads using <i>synchronized methods</i>
and <i>synchronized statements</i>. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">An
object for which access is to be coordinated is accessed through the use of
synchronized methods. These methods are declared with the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">synchronized</span></tt><span style="color:windowtext" lang="EN-US"> keyword. Only one synchronized method can
be invoked for an object at a given point in time. This keeps synchronized
methods in multiple threads from conflicting with each other. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">All
classes and objects are associated with a unique <i>monitor</i>. The monitor is
used to control the way in which synchronized methods are allowed to access the
class or object. When a synchronized method is invoked for a given object, it
is said to <i>acquire</i> the monitor for that object. No other synchronized
method may be invoked for that object until the monitor is released. A monitor
is automatically released when the method completes its execution and returns.
A monitor may also be released when a synchronized method executes certain
methods, such as </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">wait()</span></tt><span style="color:windowtext" lang="EN-US">.
The thread associated with the currently executing synchronized method becomes
not runnable until the wait condition is satisfied and no other method has
acquired the object's monitor. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The
following example shows how synchronized methods and object monitors are used
to coordinate access to a common object by multiple threads. This example
adapts the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US"> program for use with synchronized methods,
as shown in Listing 8.3. </span></p>

<p class="MsoNormal" style="margin-right:36.0pt"><b><span lang="EN-US">The source
code of the </span></b><tt><b><span style="font-size:10.0pt" lang="EN-US">ThreadSynchronization</span></b></tt><b><span lang="EN-US"> program.</span></b><span lang="EN-US"> </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">
ThreadSynchronization </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">static</span> <span style="color:blue">void</span>
main(String args[]) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyThread thread1 = <span style="color:blue">new</span> MyThread(<span style="color:green">"thread1:
"</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyThread thread2 = <span style="color:blue">new</span> MyThread(<span style="color:green">"thread2:
"</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread1.start();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread2.start();</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean
thread1IsAlive = <span style="color:blue">true</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean
thread2IsAlive = <span style="color:blue">true</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">do</span> {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">if </span>(thread1IsAlive &amp;&amp; !thread1.isAlive()) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread1IsAlive
= <span style="color:blue">false</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Thread 1
is dead."</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">if </span>(thread2IsAlive &amp;&amp; !thread2.isAlive()) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thread2IsAlive
= <span style="color:blue">false</span>;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Thread 2
is dead."</span>); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <span style="color:blue">while</span>(thread1IsAlive || thread2IsAlive);</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">}</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> MyThread extends
Thread </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{</span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">static</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> String message[]
= </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ <span style="color:green">"Java"</span>, <span style="color:green">"is"</span>,
<span style="color:green">"hot,"</span>, <span style="color:green">"aromatic,"</span>,
<span style="color:green">"and"</span>, <span style="color:green">"invigorating."</span>};</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
MyThread(String id) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(id);</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">void</span> run() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SynchronizedOutput.displayList(getName(),message);
</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">void</span>
randomWait()</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">try</span> {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep((<span style="color:blue">long</span>)(<span style="color:navy">3000</span>*Math.random()));</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <span style="color:blue">catch</span> (InterruptedException x) {</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Interrupted!"</span>);
</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; }</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">}</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">
SynchronizedOutput </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{</span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">public</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> <span style="color:blue">static</span> synchronized <span style="color:blue">void</span>
displayList(String name,String list[]) </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{</span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;
color:blue" lang="EN-US">for</span><span style="font-size:10.0pt;font-family:
&quot;Courier New&quot;" lang="EN-US">(<span style="color:blue">int</span> i=<span style="color:navy">0</span>;i&lt;list.length;++i)
{</span></p>

<p class="MsoNormal" style="margin-left:45.4pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">MyThread
t = (MyThread) Thread.currentThread(); </span></p>

<p class="MsoNormal" style="margin-left:45.4pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">t.randomWait();</span></p>

<p class="MsoNormal" style="margin-left:45.4pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">System.<span style="color:blue">out</span>.println(name+list[i]);</span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">}</span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">}</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p><span style="color:windowtext" lang="EN-US">Compile and run the program before
going on with its analysis. You might be surprised at the results that you've
obtained. Here are the results of an example run on my system: </span></p>

<p class="MsoNormal" style="margin-right:36.0pt;margin-left:36.0pt"><tt><span style="font-size:10.0pt" lang="EN-US">C:\java\jdg\ch08&gt;java
ThreadSynchronization</span></tt><span style="font-size:10.0pt;
font-family:&quot;Courier New&quot;" lang="EN-US"><br>
<tt>thread1: Java</tt><br>
<tt>thread1: is</tt><br>
<tt>thread1: hot,</tt><br>
<tt>thread1: aromatic,</tt><br>
<tt>thread1: and</tt><br>
<tt>thread1: invigorating.</tt><br>
<tt>Thread 1 is dead.</tt><br>
<tt>thread2: Java</tt><br>
<tt>thread2: is</tt><br>
<tt>thread2: hot,</tt><br>
<tt>thread2: aromatic,</tt><br>
<tt>thread2: and</tt><br>
<tt>thread2: invigorating.</tt><br>
<tt>Thread 2 is dead.</tt></span><span lang="EN-US"> </span></p>

<p class="MsoNormal" style="text-align:justify"><span lang="EN-US">Now edit </span><tt><span style="font-size:10.0pt" lang="EN-US">ThreadSynchronization.java</span></tt><span lang="EN-US"> and delete the </span><tt><span style="font-size:10.0pt" lang="EN-US">synchronized</span></tt><span lang="EN-US"> keyword in the declaration of the </span><tt><span style="font-size:10.0pt" lang="EN-US">displayList()</span></tt><span lang="EN-US"> method of
class </span><tt><span style="font-size:10.0pt" lang="EN-US">SynchronizedOutput</span></tt><span lang="EN-US">. Save </span><tt><span style="font-size:10.0pt" lang="EN-US">ThreadSynchronization.java</span></tt><span lang="EN-US">, recompile it, and rerun it with the new change in place. You may
now get output similar to this: </span></p>

<p class="MsoNormal" style="margin-right:36.0pt;margin-left:36.0pt"><tt><span style="font-size:10.0pt" lang="EN-US">C:\java\jdg\ch08&gt;java
ThreadSynchronization</span></tt><span style="font-size:10.0pt;
font-family:&quot;Courier New&quot;" lang="EN-US"><br>
<tt>thread2: Java</tt><br>
<tt>thread1: Java</tt><br>
<tt>thread1: is</tt><br>
<tt>thread2: is</tt><br>
<tt>thread2: hot,</tt><br>
<tt>thread2: aromatic,</tt><br>
<tt>thread1: hot,</tt><br>
<tt>thread2: and</tt><br>
<tt>thread2: invigorating.</tt><br>
<tt>Thread 2 is dead.</tt><br>
<tt>thread1: aromatic,</tt><br>
<tt>thread1: and</tt><br>
<tt>thread1: invigorating.</tt><br>
<tt>Thread 1 is dead.</tt></span><span lang="EN-US"> </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The
difference in the program's output should give you a feel for the effects of
synchronization upon multithreaded program execution. Let's analyze the program
and explain these results. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadSynchronization</span></tt><span style="color:windowtext" lang="EN-US"> class is essentially the same as the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US"> class. The only difference is the class
name. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">MyThread</span></tt><span style="color:windowtext" lang="EN-US"> class was modified slightly to allow for the
use of the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">SynchronizedOutput</span></tt><span style="color:windowtext" lang="EN-US"> class. Instead of the output being
displayed in the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US">
method, as in </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">ThreadTest1</span></tt><span style="color:windowtext" lang="EN-US">, the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">run()</span></tt><span style="color:windowtext" lang="EN-US"> method simply invokes the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> method of the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">SynchronizedOutput</span></tt><span style="color:windowtext" lang="EN-US"> class. It is important to understand that
the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> method is </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">static</span></tt><span style="color:windowtext" lang="EN-US"> and applies to the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">SynchronizedOutput</span></tt><span style="color:windowtext" lang="EN-US"> class as a whole, not to any particular
instance of the class. The method displays the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Java is hot, aromatic, and
invigorating.</span></tt><span style="color:windowtext" lang="EN-US"> message in
the same manner as it was displayed in the previous examples of this chapter.
It invokes </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">randomWait()</span></tt><span style="color:windowtext" lang="EN-US"> to wait a random amount of time before
displaying each word in the message. The </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> method uses the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">currentThread()</span></tt><span style="color:windowtext" lang="EN-US"> method of class </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">Thread</span></tt><span style="color:windowtext" lang="EN-US"> to reference the current thread in order
to invoke </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">randomWait()</span></tt><span style="color:windowtext" lang="EN-US">. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">What
difference, then, does the fact that </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> is synchronized have on the program's
execution? When </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> is not synchronized, it may be invoked by
one thread, say </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US">, display some output, and wait while </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> executes. When </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> executes, it too invokes </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> to display some output. Two separate
invocations of </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US">, one for </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> and the other for </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US">, execute concurrently. This explains the mixed output
display. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">When
the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">synchronized</span></tt><span style="color:windowtext" lang="EN-US"> keyword is used, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> invokes </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US">, acquires a monitor for the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">SynchronizedOutput</span></tt><span style="color:windowtext" lang="EN-US"> class (because </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> is a </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">static</span></tt><span style="color:windowtext" lang="EN-US"> method), and </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> proceeds with the output display for </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US">. Because </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread1</span></tt><span style="color:windowtext" lang="EN-US"> acquired a monitor for the </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">SynchronizedOutput</span></tt><span style="color:windowtext" lang="EN-US"> class, </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">thread2</span></tt><span style="color:windowtext" lang="EN-US"> must wait until the monitor is released before it is
able to invoke </span><tt><span style="font-size:10.0pt;color:windowtext" lang="EN-US">displayList()</span></tt><span style="color:windowtext" lang="EN-US"> to display its output. This explains why
one task's output is completed before the other's. </span></p>

<h2><span style="color:windowtext" lang="EN-US">Daemon Threads</span></h2>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">Java
borrows the notion of a daemon thread from the UNIX daemon process. A <i>daemon
thread</i> is a thread that executes in the background and provides services to
other threads. It typically executes a continuous loop of instructions that
wait for a service request, perform the service, and wait for the next service
request. Daemon threads continue to execute until there are no more threads for
which services can be provided. At this time, the daemon threads die and the
Java interpreter terminates its execution. Any thread can be changed to a
daemon thread using the </span><tt><span style="font-size:10.0pt;
color:windowtext" lang="EN-US">setDaemon()</span></tt><span style="color:windowtext" lang="EN-US">
method. </span></p>

<h2><span style="color:windowtext" lang="EN-US">Thread Groups</span></h2>

<p style="text-align:justify"><i><span style="color:windowtext" lang="EN-US">Thread
groups</span></i><span style="color:windowtext" lang="EN-US"> are objects that
consist of a collection of threads. Every thread is a member of a unique thread
group. Thread groups are used to invoke methods that apply to all threads in
the group. For example, a thread group can be used to start or stop all threads
in a group, to change their priorities, or to change them to daemon threads. </span></p>

<p style="text-align:justify"><span style="color:windowtext" lang="EN-US">A
thread is entered into a thread group when it is created. After the thread
enters a thread group, it remains a member of the group throughout its
existence. A thread can never become a member of another group. </span></p>

<p class="MsoNormal" style="text-align:justify"><span lang="EN-US">Threads are
entered into a group using </span><tt><span style="font-size:10.0pt" lang="EN-US">Thread</span></tt><span lang="EN-US"> constructors that take a </span><tt><span style="font-size:10.0pt" lang="EN-US">ThreadGroup</span></tt><span lang="EN-US"> parameter.
These constructors are described in the </span><tt><span style="font-size:10.0pt" lang="EN-US">Thread</span></tt><span lang="EN-US"> class API
documentation. If a thread's group is not specified in its constructor, as is
the usual case, the thread is entered into the same group as the thread that
created it. The default thread group for a newly executing Java application is
the </span><tt><span style="font-size:10.0pt" lang="EN-US">main</span></tt><span lang="EN-US"> group. </span></p>

<h2><span lang="EN-US">A Thread's Life</span></h2>

<p class="para" style="text-align:justify"><span lang="EN-US">A </span><tt><span style="font-size:10.0pt" lang="EN-US">Thread</span></tt><span lang="EN-US"> continues
to execute until one of the following things happens: </span></p>

<ul type="disc">
 <li class="MsoNormal" style="text-align:justify"><span lang="EN-US">It returns
     from its target </span><tt><span style="font-size:10.0pt" lang="EN-US">run()</span></tt><span lang="EN-US"> method </span></li>
 <li class="MsoNormal" style="text-align:justify"><span lang="EN-US">It's
     interrupted by an uncaught exception </span></li>
 <li class="MsoNormal" style="text-align:justify"><span lang="EN-US">Its </span><tt><span style="font-size:10.0pt" lang="EN-US">stop()</span></tt><span lang="EN-US">
     method is called </span></li>
</ul>

<p class="para" style="text-align:justify"><span lang="EN-US">So what happens if
the </span><tt><span style="font-size:10.0pt" lang="EN-US">run()</span></tt><span lang="EN-US"> method for a thread never terminates, and the application that
started the thread never calls its </span><tt><span style="font-size:10.0pt" lang="EN-US">stop()</span></tt><span lang="EN-US"> method? The answer
is that the thread lives on, even after the application that created it has
finished. This means we have to be aware of how our threads eventually
terminate, or an application can end up leaving orphaned threads that
unnecessarily consume resources. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">In many cases, what
we really want is to create background threads that do simple, periodic tasks
in an application. The </span><tt><span style="font-size:10.0pt" lang="EN-US">setDaemon()</span></tt><span lang="EN-US"> method can be used to mark a </span><tt><span style="font-size:10.0pt" lang="EN-US">Thread</span></tt><span lang="EN-US"> as a daemon thread
that should be killed and discarded when no other application threads remain.
Normally, the Java interpreter continues to run until all threads have
completed. But when daemon threads are the only threads still alive, the
interpreter will exit. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Here's a devilish
example of using daemon threads: </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> Devil extends
Thread { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; Devil() { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setDaemon( <span style="color:blue">true</span> ); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">void</span> run() { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Perform evil tasks </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">In the above example,
the </span><tt><span style="font-size:10.0pt" lang="EN-US">Devil</span></tt><span lang="EN-US"> thread sets its daemon status when it is created. If any </span><tt><span style="font-size:10.0pt" lang="EN-US">Devil</span></tt><span lang="EN-US"> threads
remain when our application is otherwise complete, Java kills them for us. We
don't have to worry about cleaning them up. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Daemon threads are
primarily useful in standalone Java applications and in the implementation of
the Java system itself, but not in applets. Since an applet runs inside of
another Java application, any daemon threads it creates will continue to live
until the controlling application exits--probably not the desired effect. </span></p>

<h2><span lang="EN-US">More about Thread Synchronization</span></h2>

<p class="para" style="text-align:justify"><span lang="EN-US">Every thread has a
life of its own. Normally, a thread goes about its business without any regard
for what other threads in the application are doing. Threads may be
time-sliced, which means they can run in arbitrary spurts and bursts as
directed by the operating system. On a multiprocessor system, it is even
possible for many different threads to be running simultaneously on different
CPUs. This section is about coordinating the activities of two or more threads,
so they can work together and not collide in their use of the same address
space. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Java provides a few
simple structures for synchronizing the activities of threads. They are all
based on the concept of <i>monitors</i>, a widely used synchronization scheme
developed by C.A.R. Hoare. You don't have to know the details about how
monitors work to be able to use them, but it may help you to have a picture in
mind. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">A monitor is
essentially a lock. The lock is attached to a resource that many threads may
need to access, but that should be accessed by only one thread at a time. It's
not unlike a public restroom at a gas station. If the resource is not being
used, the thread can acquire the lock and access the resource. By the same
token, if the restroom is unlocked, you can enter and lock the door. When the
thread is done, it relinquishes the lock, just as you unlock the door and leave
it open for the next person. However, if another thread already has the lock
for the resource, all other threads have to wait until the current thread
finishes and releases the lock, just as if the restroom is locked when you
arrive, you have to wait until the current occupant is done and unlocks the
door. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Fortunately, Java
makes the process of synchronizing access to resources quite easy. The language
handles setting up and acquiring locks; all you have to do is specify which
resources require locks. </span></p>

<h3><span lang="EN-US">Serializing Methods</span></h3>

<p class="para" style="text-align:justify"><span lang="EN-US">The most common need
for synchronization among threads in Java is to serialize their access to some
resource, namely an object. In other words, synchronization makes sure only one
thread at a time can perform certain activities that manipulate an object. In
Java, every object has a lock associated with it. To be more specific, every
class and every instance of a class has its own lock. The </span><tt><span style="font-size:10.0pt" lang="EN-US">synchronized</span></tt><span lang="EN-US">
keyword marks places where a thread must acquire the lock before proceeding. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">For example, say we
implemented a </span><tt><span style="font-size:10.0pt" lang="EN-US">SpeechSynthesizer</span></tt><span lang="EN-US"> class that contains a </span><tt><span style="font-size:
10.0pt" lang="EN-US">say()</span></tt><span lang="EN-US"> method. We don't want multiple
threads calling </span><tt><span style="font-size:10.0pt" lang="EN-US">say()</span></tt><span lang="EN-US"> at the same time or we wouldn't be able to understand anything
being said. So we mark the </span><tt><span style="font-size:10.0pt" lang="EN-US">say()</span></tt><span lang="EN-US"> method as synchronized, which means that a thread has to acquire
the lock on the </span><tt><span style="font-size:10.0pt" lang="EN-US">SpeechSynthesizer</span></tt><span lang="EN-US"> object before it can speak: </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">
SpeechSynthesizer </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; synchronized <span style="color:blue">void</span> say( String words ) { </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;
color:green" lang="EN-US">// Speak </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">}</span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Because </span><tt><span style="font-size:10.0pt" lang="EN-US">say()</span></tt><span lang="EN-US"> is an
instance method, a thread has to acquire the lock on the particular </span><tt><span style="font-size:10.0pt" lang="EN-US">SpeechSynthesizer</span></tt><span lang="EN-US"> instance it is using before it can invoke the </span><tt><span style="font-size:10.0pt" lang="EN-US">say()</span></tt><span lang="EN-US"> method.
When </span><tt><span style="font-size:10.0pt" lang="EN-US">say()</span></tt><span lang="EN-US"> has completed, it gives up the lock, which allows the next waiting
thread to acquire the lock and run the method. Note that it doesn't matter
whether the thread is owned by the </span><tt><span style="font-size:10.0pt" lang="EN-US">SpeechSynthesizer</span></tt><span lang="EN-US"> itself
or some other object; every thread has to acquire the same lock, that of the </span><tt><span style="font-size:10.0pt" lang="EN-US">SpeechSynthesizer</span></tt><span lang="EN-US"> instance. If </span><tt><span style="font-size:10.0pt" lang="EN-US">say()</span></tt><span lang="EN-US"> were a class (static) method instead of an instance method, we
could still mark it as synchronized. But in this case as there is no instance
object involved, the lock would be on the class object itself. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Often, you want to
synchronize multiple methods of the same class, so that only one of the methods
modifies or examines parts of the class at a time. All static synchronized
methods in a class use the same class object lock. By the same token, all
instance methods in a class use the same instance object lock. In this way,
Java can guarantee that only one of a set of synchronized methods is running at
a time. For example, a </span><tt><span style="font-size:10.0pt" lang="EN-US">SpreadSheet</span></tt><span lang="EN-US"> class might contain a number of instance variables that represent
cell values, as well as some methods that manipulate the cells in a row: </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> SpreadSheet </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">int</span>
cellA1, cellA2, cellA3; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; synchronized <span style="color:blue">int</span> sumRow() </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">return</span> cellA1 + cellA2 + cellA3; </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; synchronized <span style="color:blue">void</span> setRow( <span style="color:blue">int</span> a1, <span style="color:blue">int</span> a2, <span style="color:blue">int</span> a3 ) </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">cellA1
= a1; </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">cellA2
= a2; </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">cellA3
= a3; </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; ... </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">In this example, both
methods </span><tt><span style="font-size:10.0pt" lang="EN-US">setRow()</span></tt><span lang="EN-US"> and </span><tt><span style="font-size:10.0pt" lang="EN-US">sumRow()</span></tt><span lang="EN-US"> access the cell values. You can see that problems might arise if
one thread were changing the values of the variables in </span><tt><span style="font-size:10.0pt" lang="EN-US">setRow()</span></tt><span lang="EN-US"> at
the same moment another thread was reading the values in </span><tt><span style="font-size:10.0pt" lang="EN-US">sumRow()</span></tt><span lang="EN-US">. To
prevent this, we have marked both methods as synchronized. When threads are
synchronized, only one will be run at a time. If a thread is in the middle of
executing </span><tt><span style="font-size:10.0pt" lang="EN-US">setRow()</span></tt><span lang="EN-US"> when another thread calls </span><tt><span style="font-size:10.0pt" lang="EN-US">sumRow()</span></tt><span lang="EN-US">, the second
thread waits until the first one is done executing </span><tt><span style="font-size:10.0pt" lang="EN-US">setRow()</span></tt><span lang="EN-US"> before it gets
to run </span><tt><span style="font-size:10.0pt" lang="EN-US">sumRow()</span></tt><span lang="EN-US">. This synchronization allows us to preserve the consistency of the </span><tt><span style="font-size:10.0pt" lang="EN-US">SpreadSheet</span></tt><span lang="EN-US">.
And the best part is that all of this locking and waiting is handled by Java;
it's transparent to the programmer. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">In addition to
synchronizing entire methods, the </span><tt><span style="font-size:
10.0pt" lang="EN-US">synchronized</span></tt><span lang="EN-US"> keyword can be used in a
special construct to guard arbitrary blocks of code. In this form it also takes
an explicit argument that specifies the object for which it is to acquire a
lock: </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">synchronized ( myObject ) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:green">//
Functionality that needs to be synced </span></span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">... </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">The code block above
can appear in any method. When it is reached, the thread has to acquire the
lock on </span><tt><span style="font-size:10.0pt" lang="EN-US">myObject</span></tt><span lang="EN-US"> before proceeding. In this way, we can have methods (or parts of
methods) in different classes synchronized the same as methods in the same
class. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">A synchronized method
is, therefore, equivalent to a method with its statements synchronized on the
current object. Thus: </span></p>

<pre><span lang="EN-US">synchronized void myMethod () { </span></pre><pre><span lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;... </span></pre><pre><span lang="EN-US">} </span></pre>

<p class="para"><span lang="EN-US">is equivalent to: </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">void</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> myMethod () </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">synchronized ( <span style="color:blue">this</span> ) </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">... </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<h2><span lang="EN-US">Using wait(&nbsp;) and notify(&nbsp;)</span></h2>

<p class="para" style="text-align:justify"><span lang="EN-US">With the </span><tt><span style="font-size:10.0pt" lang="EN-US">synchronized</span></tt><span lang="EN-US">
keyword, we can serialize the execution of complete methods and blocks of code.
The </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> and </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> methods of the </span><tt><span style="font-size:10.0pt" lang="EN-US">Object</span></tt><span lang="EN-US"> class extend this capability. Every object in Java is a subclass of
</span><tt><span style="font-size:10.0pt" lang="EN-US">Object</span></tt><span lang="EN-US">, so every object inherits these methods. By using </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> and </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US">, a
thread can give up its hold on a lock at an arbitrary point, and then wait for
another thread to give it back before continuing. All of the coordinated
activity still happens inside of synchronized blocks, and still only one thread
is executing at a given time. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">By executing </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> from a
synchronized block, a thread gives up its hold on the lock and goes to sleep. A
thread might do this if it needs to wait for something to happen in another
part of the application, as you'll see shortly. Later, when the necessary event
happens, the thread that is running it calls </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> from a block
synchronized on the same object. Now the first thread wakes up and begins
trying to acquire the lock again. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">When the first thread
manages to reacquire the lock, it continues from the point it left off.
However, the thread that waited may not get the lock immediately (or perhaps
ever). It depends on when the second thread eventually releases the lock, and
which thread manages to snag it next. Note also, that the first thread won't
wake up from the </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> unless another thread calls </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US">. There is an
overloaded version of </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US">, however, that allows us to specify a timeout period. If another
thread doesn't call </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> in the specified period, the waiting thread automatically wakes up.
</span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Let's look at a
simple scenario to see what's going on. In the following example, we'll assume
there are three threads--one waiting to execute each of the three synchronized
methods of the </span><tt><span style="font-size:10.0pt" lang="EN-US">MyThing</span></tt><span lang="EN-US"> class. We'll call them the </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US">, </span><tt><span style="font-size:10.0pt" lang="EN-US">notifier</span></tt><span lang="EN-US">, and </span><tt><span style="font-size:10.0pt" lang="EN-US">related</span></tt><span lang="EN-US">
threads, respectively. Here's a code fragment to illustrate: </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> MyThing </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; synchronized <span style="color:blue">void</span> waiterMethod() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Do some stuff </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Now we need to wait for notifier to do something </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wait(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Continue where we left off </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; synchronized <span style="color:blue">void</span> notifierMethod() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Do some stuff&nbsp; </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Notify waiter that we've done it </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; notify(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Do more things </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; synchronized <span style="color:blue">void</span> relatedMethod() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Do some related stuff </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">}</span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Let's assume </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US"> gets
through the gate first and begins executing </span><tt><span style="font-size:10.0pt" lang="EN-US">waiterMethod()</span></tt><span lang="EN-US">. The two
other threads are initially blocked, trying to acquire the lock for the </span><tt><span style="font-size:10.0pt" lang="EN-US">MyThing</span></tt><span lang="EN-US">
object. When </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US"> executes the </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> method, it relinquishes its hold on the lock and goes to sleep. Now
there are now two viable threads waiting for the lock. Which thread gets it
depends on several factors, including chance and the priorities of the threads.
(We'll discuss thread scheduling in the next section.) </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Let's say that </span><tt><span style="font-size:10.0pt" lang="EN-US">notifier</span></tt><span lang="EN-US"> is
the next thread to acquire the lock, so it begins to run. </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US">
continues to sleep and </span><tt><span style="font-size:10.0pt" lang="EN-US">related</span></tt><span lang="EN-US"> languishes, waiting for its turn. When </span><tt><span style="font-size:10.0pt" lang="EN-US">notifier</span></tt><span lang="EN-US"> executes the
call to </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US">, Java prods the </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US"> thread, effectively telling it something has changed. </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US"> then
wakes up and rejoins </span><tt><span style="font-size:10.0pt" lang="EN-US">related</span></tt><span lang="EN-US"> in vying for the </span><tt><span style="font-size:10.0pt" lang="EN-US">MyThing</span></tt><span lang="EN-US"> lock. Note that it doesn't actually receive the lock; it just changes
from saying "leave me alone" to "I want the lock." </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">At this point, </span><tt><span style="font-size:10.0pt" lang="EN-US">notifier</span></tt><span lang="EN-US"> still
owns the lock and continues to hold it until it leaves its synchronized method
(or perhaps executes a </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> itself&nbsp;). When it finally completes, the other two methods get
to fight over the lock. </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US"> would like to continue executing </span><tt><span style="font-size:10.0pt" lang="EN-US">waiterMethod()</span></tt><span lang="EN-US"> from the
point it left off, while </span><tt><span style="font-size:10.0pt" lang="EN-US">unrelated</span></tt><span lang="EN-US">, which has been patient, would like to get started. We'll let you
choose your own ending for the story. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">For each call to </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US">, Java
wakes up just one method that is asleep in a </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> call. If there are
multiple threads waiting, Java picks the first thread on a first-in, first-out
basis. The </span><tt><span style="font-size:10.0pt" lang="EN-US">Object</span></tt><span lang="EN-US"> class also provides a </span><tt><span style="font-size:
10.0pt" lang="EN-US">notifyAll()</span></tt><span lang="EN-US"> call to wake up all waiting
threads. In most cases, you'll probably want to use </span><tt><span style="font-size:10.0pt" lang="EN-US">notifyAll()</span></tt><span lang="EN-US">
rather than </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US">. Keep in mind that </span><tt><span style="font-size:
10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> really means "Hey, something
related to this object has changed. The condition you are waiting for may have
changed, so check it again." In general, there is no reason to assume only
one thread at a time is interested in the change or able to act upon it.
Different threads might look upon whatever has changed in different ways. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Often, our </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US"> thread
is waiting for a particular condition to change and we will want to sit in a
loop like the following: </span></p>

<pre style="text-align:justify"><span lang="EN-US">... </span></pre>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">while</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> ( condition != <span style="color:blue">true</span> ) </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">wait(); </span></p>

<pre style="text-align:justify"><span lang="EN-US">... </span></pre>

<p class="para" style="text-align:justify"><span lang="EN-US">Other synchronized
threads call </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> or </span><tt><span style="font-size:10.0pt" lang="EN-US">notifyAll()</span></tt><span lang="EN-US"> when they have modified the environment so that </span><tt><span style="font-size:10.0pt" lang="EN-US">waiter</span></tt><span lang="EN-US"> can
check the condition again. This is the civilized alternative to polling and
sleeping, as you'll see the following example. </span></p>

<h3><span lang="EN-US">The Message Passer (Producer/Consumer Problem)</span></h3>

<p class="para" style="text-align:justify"><span lang="EN-US">Now we'll illustrate
a classic interaction between two threads: a </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> and a </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US">. A
producer thread creates messages and places them into a queue, while a consumer
reads them out and displays them. To be realistic, we'll give the queue a
maximum depth. And to make things really interesting, we'll have our consumer
thread be lazy and run much slower than the producer. This means that </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US">
occasionally has to stop and wait for </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> to catch up. The
example below shows the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> and </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> classes. </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">import java.util.Vector; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> Producer extends
Thread { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">static</span>
final <span style="color:blue">int</span> MAXQUEUE = <span style="color:navy">5</span>;
</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">private</span>
Vector messages = <span style="color:blue">new</span> Vector(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">void</span> run() { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">try</span> { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">while</span> ( <span style="color:blue">true</span> ) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
putMessage(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep( <span style="color:navy">1000</span> ); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">catch</span>( InterruptedException e ) { } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">private</span>
synchronized <span style="color:blue">void</span> putMessage() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throws
InterruptedException </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">while</span> ( messages.size() == MAXQUEUE ) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wait(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; messages.addElement(
<span style="color:blue">new</span> java.util.Date().toString() ); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; notify(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:green">//
Called by Consumer </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
synchronized String getMessage() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throws
InterruptedException </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; notify(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">while</span> ( messages.size() == <span style="color:navy">0</span>
) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wait(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String message =
(String)messages.firstElement(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; messages.removeElement(
message ); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">return</span> message; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;</span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> Consumer extends
Thread { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; Producer producer; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; Consumer(Producer p) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; producer = p; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">void</span> run() { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">try</span> { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">while</span> ( <span style="color:blue">true</span> ) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String
message = producer.getMessage(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(<span style="color:green">"Got
message: "</span> + message); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep( <span style="color:navy">2000</span> ); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">catch</span>( InterruptedException e ) { } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">static</span> <span style="color:blue">void</span>
main(String args[]) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Producer producer = <span style="color:blue">new</span> Producer(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; producer.start(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">new</span> Consumer( producer ).start(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">For convenience, we
have included a </span><tt><span style="font-size:10.0pt" lang="EN-US">main()</span></tt><span lang="EN-US"> method that runs the complete example in the </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US">
class. It creates a </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> that is tied to a </span><tt><span style="font-size:
10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> and starts the two classes. You
can run the example as follows: </span></p>

<pre style="text-align:justify"><span lang="EN-US">% java Consumer</span></pre>

<p class="para" style="text-align:justify"><span lang="EN-US">The output is the
time-stamp messages created by the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US">: </span></p>

<pre style="text-align:justify"><span lang="EN-US">Got message: Sun Dec 19 03:35:55 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">Got message: Sun Dec 19 03:35:56 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">Got message: Sun Dec 19 03:35:57 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">... </span></pre>

<p class="para" style="text-align:justify"><span lang="EN-US">The time stamps
initially show a spacing of one second, although they appear every two seconds.
Our </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> runs faster than our </span><tt><span style="font-size:
10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US">. </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> would like to
generate a new message every second, while </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> gets around to
reading and displaying a message only every two seconds. Can you see how long
it will take the message queue to fill up? What will happen when it does? </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Let's look at the
code. We are using a few new tools here. </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> and </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> are
subclasses of </span><tt><span style="font-size:10.0pt" lang="EN-US">Thread</span></tt><span lang="EN-US">. It would have been a better design decision to have </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> and </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US">
implement the </span><tt><span style="font-size:10.0pt" lang="EN-US">Runnable</span></tt><span lang="EN-US"> interface, but we took the slightly easier path and subclassed </span><tt><span style="font-size:10.0pt" lang="EN-US">Thread</span></tt><span lang="EN-US">. You
should find it fairly simple to use the other technique; you might try it as an
exercise. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">The </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> and </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US">
classes pass messages through an instance of a </span><tt><span style="font-size:10.0pt" lang="EN-US">java.util.Vector</span></tt><span lang="EN-US"> object.
We haven't discussed the </span><tt><span style="font-size:10.0pt" lang="EN-US">Vector</span></tt><span lang="EN-US"> class yet, but you can think of this one as a queue where we add
and remove elements in first-in, first-out order.</span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">The important
activity is in the synchronized methods: </span><tt><span style="font-size:10.0pt" lang="EN-US">putMessage()</span></tt><span lang="EN-US"> and </span><tt><span style="font-size:10.0pt" lang="EN-US">getMessage()</span></tt><span lang="EN-US">.
Although one of the methods is used by the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> thread and the
other by the </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> thread, they both live in the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> class because
they have to be synchronized on the same object to work together. Here they
both implicitly use the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> object's lock. If the queue is empty, the </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US">
blocks in a call in the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US">, waiting for another message. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Another design option
would implement the </span><tt><span style="font-size:10.0pt" lang="EN-US">getMessage()</span></tt><span lang="EN-US"> method in the </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> class and use a synchronized code block to explicitly synchronize
on the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> object. In either case, synchronizing on the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> is
important because it allows us to have multiple </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> objects that
feed on the same </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US">. </span></p>

<p class="para" style="text-align:justify"><tt><span style="font-size:
10.0pt" lang="EN-US">putMessage()</span></tt><span lang="EN-US">'s job is to add a new message
to the queue. It can't do this if the queue is already full, so it first checks
the number of elements in </span><tt><span style="font-size:10.0pt" lang="EN-US">messages</span></tt><span lang="EN-US">. If there is room, it stuffs in another time stamp. If the queue is
at its limit however, </span><tt><span style="font-size:10.0pt" lang="EN-US">putMessage()</span></tt><span lang="EN-US"> has to wait until there's space. In this situation, </span><tt><span style="font-size:10.0pt" lang="EN-US">putMessage()</span></tt><span lang="EN-US">
executes a </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> and relies on the consumer to call </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> to wake it up
after a message has been read. Here we have </span><tt><span style="font-size:10.0pt" lang="EN-US">putMessage()</span></tt><span lang="EN-US"> testing the
condition in a loop. In this simple example, the test probably isn't necessary;
we could assume that when </span><tt><span style="font-size:10.0pt" lang="EN-US">putMessage()</span></tt><span lang="EN-US"> wakes up, there is a free spot. However, this test is another
example of good programming practice. Before it finishes, </span><tt><span style="font-size:10.0pt" lang="EN-US">putMessage()</span></tt><span lang="EN-US">
calls </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> itself to prod any </span><tt><span style="font-size:
10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> that might be waiting on an empty
queue. </span></p>

<p class="para" style="text-align:justify"><tt><span style="font-size:
10.0pt" lang="EN-US">getMessage()</span></tt><span lang="EN-US"> retrieves a message for the </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US">. It
enters a loop like the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US">'s, waiting for the queue to have at least one element before
proceeding. If the queue is empty, it executes a </span><tt><span style="font-size:10.0pt" lang="EN-US">wait()</span></tt><span lang="EN-US"> and expects the
producer to call </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> when more items are available. Notice that </span><tt><span style="font-size:10.0pt" lang="EN-US">getMessage()</span></tt><span lang="EN-US">
makes its own unconditional call to </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US">. This is a
somewhat lazy way of keeping the </span><tt><span style="font-size:
10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> on its toes, so that the queue
should generally be full. Alternatively, </span><tt><span style="font-size:10.0pt" lang="EN-US">getMessage()</span></tt><span lang="EN-US"> might test
to see if the queue had fallen below a low water mark before waking up the
producer. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Now let's add another
</span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> to the scenario, just to make things really interesting. Most of
the necessary changes are in the </span><tt><span style="font-size:
10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> class; the example below shows
the code for the modified class. </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue" lang="EN-US">class</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US"> Consumer extends
Thread </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">{ </span></p>

<p class="MsoNormal" style="text-indent:22.7pt;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">Producer
producer; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; String name; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; Consumer(String name,
Producer producer) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">this</span>.producer = producer; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">this</span>.name = name; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">void</span> run() </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">try</span> { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">while</span> ( <span style="color:blue">true</span> ) { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String
message = producer.getMessage(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<span style="color:blue">out</span>.println(name + <span style="color:green">"
got message: "</span> + message); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep( <span style="color:navy">2000</span> ); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <span style="color:blue">catch</span>( InterruptedException e ) { </span></p>

<p class="MsoNormal" style="margin-left:22.7pt;text-indent:22.7pt;text-autospace:
none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; <span style="color:blue">public</span>
<span style="color:blue">static</span> <span style="color:blue">void</span>
main(String args[]) </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; { </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Producer producer = <span style="color:blue">new</span> Producer(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; producer.start(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Start two this time </span></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">new</span> Consumer( <span style="color:green">"One"</span>,
producer ).start(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">new</span> Consumer( <span style="color:green">"Two"</span>,
producer ).start(); </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">&nbsp;&nbsp;&nbsp; } </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;" lang="EN-US">} </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">The </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US">
constructor now takes a string name, to identify each consumer. The </span><tt><span style="font-size:10.0pt" lang="EN-US">run()</span></tt><span lang="EN-US"> method
uses this name in the call to </span><tt><span style="font-size:
10.0pt" lang="EN-US">println()</span></tt><span lang="EN-US"> to identify which consumer
received the message. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">The only modification
to make in the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> code is to change the call to </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> in </span><tt><span style="font-size:10.0pt" lang="EN-US">putMessage()</span></tt><span lang="EN-US">
to a call to </span><tt><span style="font-size:10.0pt" lang="EN-US">notifyAll()</span></tt><span lang="EN-US">. Now, instead of the consumer and producer playing tag with the
queue, we can have many players waiting on the condition of the queue to
change. We might have a number of consumers waiting for a message, or we might
have the producer waiting for a consumer to take a message. Whenever the
condition of the queue changes, we prod all of the waiting methods to
reevaluate the situation by calling </span><tt><span style="font-size:10.0pt" lang="EN-US">notifyAll()</span></tt><span lang="EN-US">. Note,
however, that we don't need to change the call to </span><tt><span style="font-size:10.0pt" lang="EN-US">notify()</span></tt><span lang="EN-US"> in </span><tt><span style="font-size:10.0pt" lang="EN-US">getMessage()</span></tt><span lang="EN-US">.
If a </span><tt><span style="font-size:10.0pt" lang="EN-US">Consumer</span></tt><span lang="EN-US"> thread is waiting for a message to appear in the queue, it's not
possible for the </span><tt><span style="font-size:10.0pt" lang="EN-US">Producer</span></tt><span lang="EN-US"> to be simultaneously waiting because the queue is full. </span></p>

<p class="para" style="text-align:justify"><span lang="EN-US">Here is some sample
output when there are two consumers running, as in the </span><tt><span style="font-size:10.0pt" lang="EN-US">main()</span></tt><span lang="EN-US"> method
shown above: </span></p>

<pre style="text-align:justify"><span lang="EN-US">One got message: Wed Mar 20 20:00:01 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">Two got message: Wed Mar 20 20:00:02 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">One got message: Wed Mar 20 20:00:03 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">Two got message: Wed Mar 20 20:00:04 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">One got message: Wed Mar 20 20:00:05 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">Two got message: Wed Mar 20 20:00:06 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">One got message: Wed Mar 20 20:00:07 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">Two got message: Wed Mar 20 20:00:08 CST 1996 </span></pre><pre style="text-align:justify"><span lang="EN-US">... </span></pre>

<p class="para" style="text-align:justify"><span lang="EN-US">We see nice, orderly
alternation between the two consumers, as a result of the calls to </span><tt><span style="font-size:10.0pt" lang="EN-US">sleep()</span></tt><span lang="EN-US"> in the
various methods. Interesting things would happen, however, if we were to remove
all of the calls to </span><tt><span style="font-size:10.0pt" lang="EN-US">sleep()</span></tt><span lang="EN-US"> and let things run at full speed. The threads would compete and
their behavior would depend on whether or not the system is using time slicing.
On a time-sliced system, there should be a fairly random distribution between
the two consumers, while on a nontime-sliced system, a single consumer could
monopolize the messages. And since you're probably wondering about time
slicing, let's talk about thread priority and scheduling.</span></p>

</div>




</body></html>